package com.haitonghop.requester;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.haitonghop.data.HaiBLMetalData;
import com.haitonghop.data.HaiBLModule;
import com.haitonghop.data.TamDiemDetailModuleData;
import com.haitonghop.network.Request;
import com.haitonghop.network.RequestListener;
import com.haitonghop.network.UrlHelper;
import com.haitonghop.utils.AppSettingParams;

public class GirlRequester extends Request {

	private String method = "";
	private static String keyRandom = "";
	private int page = 1;
	private String id = "";
	private RequestListener requestListener;

	// Data
	ArrayList<HaiBLMetalData> haiVLMetalDatas = new ArrayList<HaiBLMetalData>();
	private ArrayList<TamDiemDetailModuleData> tamDiemDetailModuleDataDatas = new ArrayList<TamDiemDetailModuleData>();

	public void setMethod(String aMethod) {
		this.method = aMethod;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public void setRequestListener(RequestListener requestListener) {
		this.requestListener = requestListener;
	}

	@Override
	public void cancelRequest() {
		// TODO Auto-generated method stub
	}

	private static final int COLLECTION_GIRL = 114;
	private static final int COLLECTION_GIRL_DETAIL = 115;
	private static final int COLLECTION_VIDEO = 116;
	int state = -1;

	public static GirlRequester getGirlCollection(int page, int count, RequestListener requestListener) {

		GirlRequester girlRequester = new GirlRequester();
		girlRequester.state = COLLECTION_GIRL;
		girlRequester.page = page;
		girlRequester.requestListener = requestListener;

		return girlRequester;
	}

	public static GirlRequester getGirlCollectionDetail(String id, RequestListener requestListener) {

		GirlRequester girlRequester = new GirlRequester();
		girlRequester.state = COLLECTION_GIRL_DETAIL;
		girlRequester.id = id;
		girlRequester.requestListener = requestListener;

		return girlRequester;
	}

	public static GirlRequester getDetail(String url, RequestListener requestListener) {
		GirlRequester girlRequester = new GirlRequester();
		girlRequester.method = url;
		girlRequester.requestListener = requestListener;
		return girlRequester;
	}

	public static GirlRequester getVideo(int page, int count, RequestListener requestListener) {
		GirlRequester girlRequester = new GirlRequester();
		girlRequester.state = COLLECTION_VIDEO;
		girlRequester.page = page;
		girlRequester.requestListener = requestListener;

		return girlRequester;
	}

	@Override
	public void runRequest() {
		if (method.startsWith("http://")) {
			url = UrlHelper.getUrlOfMethod(UrlHelper.BASE_URL_DETAIL, new String[] { "url", "page", "count", "rndk" },
					new String[] { method, String.valueOf(page), String.valueOf(AppSettingParams.COUNT), GirlRequester.keyRandom });
		} else if (state == COLLECTION_GIRL) {
			// TODO Auto-generated method stub
			url = UrlHelper.getUrlOfMethod(UrlHelper.BASE_URL_GIRL_COLLECTION, new String[] { "page", "count" }, new String[] { String.valueOf(page), String.valueOf(AppSettingParams.COUNT) });
		} else if (state == COLLECTION_VIDEO) {
			// TODO Auto-generated method stub
			url = UrlHelper.getUrlOfMethod(UrlHelper.BASE_URL_GIRL_COLLECTION_VIDEO, new String[] { "page", "count" }, new String[] { String.valueOf(page), String.valueOf(AppSettingParams.COUNT) });
		} else if (state == COLLECTION_GIRL_DETAIL) {
			// TODO Auto-generated method stub
			url = UrlHelper.getUrlOfMethod(UrlHelper.BASE_URL_GIRL_COLLECTION_DETAIL, new String[] { "id" }, new String[] { String.valueOf(id) });
		} else {
			// TODO Auto-generated method stub
			url = UrlHelper.getUrlOfMethod(UrlHelper.BASE_URL_GIRL, new String[] { "method", "page", "count", "rndk" },
					new String[] { method, String.valueOf(page), String.valueOf(AppSettingParams.COUNT), GirlRequester.keyRandom });
		}

		send();
	}

	public ArrayList<HaiBLMetalData> getDataList() {
		return haiVLMetalDatas;
	}

	@Override
	public void requestFinished(JSONObject jsonObject) {
		if (state == COLLECTION_GIRL) {
			if (jsonObject.has("data")) {
				JSONArray array = null;
				try {
					array = jsonObject.getJSONArray("data");

					for (int i = 0, n = array.length(); i < n; i++) {
						JSONObject index = array.getJSONObject(i);
						TamDiemDetailModuleData detailModuleData = new TamDiemDetailModuleData();
						// smallsrc
						detailModuleData.title = index.has("title") ? index.getString("title") : "";
						detailModuleData.src = index.has("src") ? index.getString("src") : "";
						detailModuleData.avatar = index.has("src") ? index.getString("src") : "";
						detailModuleData.href = index.has("id") ? index.getString("id") : "";

						getTamDiemDetailModuleDataDatas().add(detailModuleData);
					}

					if (requestListener != null) {
						requestListener.onRequestComplete(this);
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			return;
		} else if (state == COLLECTION_VIDEO) {
			if (jsonObject.has("data")) {
				JSONArray array = null;
				try {
					array = jsonObject.getJSONArray("data");

					for (int i = 0, n = array.length(); i < n; i++) {
						JSONObject index = array.getJSONObject(i);
						TamDiemDetailModuleData detailModuleData = new TamDiemDetailModuleData();
						// smallsrc
						detailModuleData.title = index.has("title") ? index.getString("title") : "";
						detailModuleData.src = index.has("thumb") ? index.getString("thumb") : "";
						detailModuleData.avatar = index.has("thumb") ? index.getString("thumb") : "";
						detailModuleData.href = index.has("link") ? index.getString("link") : "";

						getTamDiemDetailModuleDataDatas().add(detailModuleData);
					}

					if (requestListener != null) {
						requestListener.onRequestComplete(this);
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			return;
		}
		if (method.startsWith("http://")) {
			// TamDiemDetailModuleData
			if (jsonObject.has("data")) {
				JSONArray array = null;
				try {
					array = jsonObject.getJSONArray("data");

					for (int i = 0, n = array.length(); i < n; i++) {
						JSONObject index = array.getJSONObject(i);
						TamDiemDetailModuleData detailModuleData = new TamDiemDetailModuleData();
						// smallsrc
						detailModuleData.bigsrc = index.has("bigsrc") ? index.getString("bigsrc") : "";
						detailModuleData.src = index.has("src") ? index.getString("src") : "";
						detailModuleData.thumbnailsrc = index.has("thumbnailsrc") ? index.getString("thumbnailsrc") : "";
						detailModuleData.avatar = index.has("thumbnailsrc") ? index.getString("thumbnailsrc") : "";
						detailModuleData.smallsrc = index.has("smallsrc") ? index.getString("smallsrc") : "";

						getTamDiemDetailModuleDataDatas().add(detailModuleData);
					}

					if (requestListener != null) {
						requestListener.onRequestComplete(this);
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} else {
			if (jsonObject != null && jsonObject.has("rndk")) {
				try {
					GirlRequester.keyRandom = jsonObject.getString("rndk");
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			if (jsonObject != null && jsonObject.has("data")) {
				try {
					JSONArray jsonArray = jsonObject.getJSONArray("data");
					final int size = jsonArray.length();
					for (int i = 0; i < size; i++) {
						JSONObject datas = jsonArray.getJSONObject(i);

						String imageUrl = datas.has(HaiBLMetalData.SRC) ? datas.getString(HaiBLMetalData.SRC) : "";
						Log.i("GirlRequester", "" + imageUrl);
						String detailUrl = datas.has(HaiBLMetalData.IMG) ? datas.getString(HaiBLMetalData.IMG) : "";
						String title = datas.has(HaiBLMetalData.TITLE) ? datas.getString(HaiBLMetalData.TITLE) : "";

						String uploader = datas.has(HaiBLMetalData.UPLOADER) ? datas.getString(HaiBLMetalData.UPLOADER) : "";
						String viewCount = datas.has(HaiBLMetalData.VIEW_COUNT) ? datas.getString(HaiBLMetalData.VIEW_COUNT) : "0";
						String commentCount = datas.has(HaiBLMetalData.COMMENT_COUNT) ? datas.getString(HaiBLMetalData.COMMENT_COUNT) : "0";
						String likeCount = datas.has(HaiBLMetalData.LIKE_COUNT) ? datas.getString(HaiBLMetalData.LIKE_COUNT) : "0";
						String bigImageUrl = datas.has(HaiBLMetalData.BIG_SRC) ? datas.getString(HaiBLMetalData.BIG_SRC) : "";
						boolean hasmore = datas.has("more") ? datas.getInt("more") == 1 : false;

						HaiBLMetalData haiBLMetalData = new HaiBLMetalData(imageUrl, detailUrl, title, uploader, viewCount, commentCount);
						haiBLMetalData.setLikeCount(likeCount);
						haiBLMetalData.setBigImageUrl(bigImageUrl);
						haiBLMetalData.setTYPE(HaiBLModule.GIRLXINH);
						haiBLMetalData.setHasMore(hasmore);

						Log.i("GirlRequester", "Add " + i);
						haiVLMetalDatas.add(haiBLMetalData);
					}

					if (requestListener != null) {
						requestListener.onRequestComplete(this);
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	@Override
	public void requestFailed(int errorCode, String message) {
		// TODO Auto-generated method stub
		if (requestListener != null) {
			requestListener.onRequestFailed(message, this);
		}
	}

	public ArrayList<TamDiemDetailModuleData> getTamDiemDetailModuleDataDatas() {
		return tamDiemDetailModuleDataDatas;
	}

	public void setTamDiemDetailModuleDataDatas(ArrayList<TamDiemDetailModuleData> tamDiemDetailModuleDataDatas) {
		this.tamDiemDetailModuleDataDatas = tamDiemDetailModuleDataDatas;
	}

}
